﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Web;

namespace Jiz.WebApi
{
    static class ParameterInjection
    {
        public static void Injection4Get(HttpRequest httpRequest, object parameter)
        {
            PropertyInfo[] propertyArr = parameter.GetType().GetProperties();
            foreach (PropertyInfo property in propertyArr)
            {
                ParameterAttribute paramAttribute = property.GetCustomAttribute<ParameterAttribute>();
                if (paramAttribute != null)
                {
                    string paramString = string.Empty;
                    if (string.IsNullOrEmpty(paramAttribute.Namespace))
                    {
                        if (!string.IsNullOrEmpty(paramAttribute.Name))
                        {
                            paramString = paramAttribute.Name;
                        }
                        else
                        {
                            paramString = property.Name;
                        }
                    }
                    else
                    {
                        if (!string.IsNullOrEmpty(paramAttribute.Name))
                        {
                            paramString = string.Format("{0}:{1}", paramAttribute.Namespace, paramAttribute.Name);
                        }
                        else
                        {
                            paramString = string.Format("{0}:{1}", paramAttribute.Namespace, property.Name);
                        }
                    }

                    property.SetValue(parameter, GetValue4Get(property.PropertyType, httpRequest, paramString));
                }
            }
        }

        public static void Injection4Post(HttpRequest httpRequest, object parameter)
        {
            PropertyInfo[] propertyArr = parameter.GetType().GetProperties();
            foreach (PropertyInfo property in propertyArr)
            {
                ParameterAttribute paramAttribute = property.GetCustomAttribute<ParameterAttribute>();
                if (paramAttribute != null)
                {
                    string paramString = string.Empty;
                    if (string.IsNullOrEmpty(paramAttribute.Namespace))
                    {
                        if (!string.IsNullOrEmpty(paramAttribute.Name))
                        {
                            paramString = paramAttribute.Name;
                        }
                        else
                        {
                            paramString = property.Name;
                        }
                    }
                    else
                    {
                        if (!string.IsNullOrEmpty(paramAttribute.Name))
                        {
                            paramString = string.Format("{0}:{1}", paramAttribute.Namespace, paramAttribute.Name);
                        }
                        else
                        {
                            paramString = string.Format("{0}:{1}", paramAttribute.Namespace, property.Name);
                        }
                    }

                    property.SetValue(parameter, GetValue4Post(property.PropertyType, httpRequest, paramString));
                }
            }
        }

        private static object GetValue4Get(Type valueType, HttpRequest httpRequest, string paramString)
        {
            if (valueType == typeof(string))
            {
                return httpRequest.QueryString[paramString];
            }
            else if (valueType == typeof(int))
            {
                return Convert.ToInt32(httpRequest.QueryString[paramString]);
            }
            else if (valueType == typeof(Guid))
            {
                return new Guid(httpRequest.QueryString[paramString]);
            }
            else if (valueType == typeof(DateTime))
            {
                return Convert.ToDateTime(httpRequest.QueryString[paramString]);
            }
            return httpRequest.QueryString[paramString];
        }

        private static object GetValue4Post(Type valueType, HttpRequest httpRequest, string paramString)
        {
            if (valueType == typeof(string))
            {
                return httpRequest.Form[paramString];
            }
            else if (valueType == typeof(int))
            {
                return Convert.ToInt32(httpRequest.Form[paramString]);
            }
            else if (valueType == typeof(Guid))
            {
                return new Guid(httpRequest.Form[paramString]);
            }
            else if (valueType == typeof(DateTime))
            {
                return Convert.ToDateTime(httpRequest.Form[paramString]);
            }
            else if (valueType == typeof(HttpPostedFile))
            {
                return httpRequest.Files[paramString];
            }
            return httpRequest.Form[paramString];
        }
    }
}
